
-- Author: shunguo.chen
-- Date: 2016-11-10 14:29:38
--
-- SharePanel

local ClassRef = app.mvc.createPanelClass(...)

function ClassRef.create(res)
	local sprite = ClassRef.extend(ccui.Layout:create())

	local node = cc.CSLoader:createNode(res, true)
	node:setPosition(display.cx, display.cy)
	node:setName("content")
	sprite:addChild(node)
	ccui.Helper:doLayout(node)

	sprite:init()
	sprite._res = res
	return sprite
end

function ClassRef:init(data)
	self.super.init(self, data)

	self:setContentSize(display.size)

	self._conPanel = self:getChildByName("content")

	local children = self._conPanel:findChild("Panel_1"):getChildren()
	for i,child in ipairs(children) do
		child:setVisible(false)
	end
end

function ClassRef:refresh()
	if self._data then
		-- for i,v in ipairs(self._data) do
		-- 	local shader
		-- 	if not v.comp then
		-- 		shader = "ShaderDark"
		-- 	end
		-- 	display.applySpriteShader(self._conPanel:findChild("Panel_1/boss_" .. i), shader)
		-- end

		if #self._data == 12 then
			self._data[#self._data + 1] = {achId=1, comp=false}
			self._data[#self._data + 1] = {achId=2, comp=false}
			self._data[#self._data + 1] = {achId=3, comp=false}
		end

		local i = 0
		local function showBoss()
			i = i + 1
			if i <= #self._data then
				local node = self._conPanel:findChild("Panel_1/boss_" .. i)
				if not self._data[i].comp then
					-- display.applySpriteShader(node, "ShaderDark")
					node:setColor(cc.c3b(50, 50, 50))
				end
				node:setVisible(true)
			else
				self:stopAction(self._action) 
				self._action = nil
			end
		end
		if self._action then 
			self:stopAction(self._action) 
			self._action = nil
		end
		self._action = self:schedule(showBoss, 0)
	end
end

function ClassRef:setNewTag(tag)
	print("SharePanel.setTag")
	self:setTag(tag)

	for i=1,4 do
		local back = self._conPanel:findChild("Panel_2/back_" .. i)
		if back then
			back:setVisible(i == tag)
		end
	end
	
	local bg = self._conPanel:findChild("Panel_2/base")
	if bg then
		bg:loadTexture("art/fenxiang/base_" .. tag .. ".png")
	end
end

function ClassRef:setCompData(gotNum, maxNum)
	self._gotNum = gotNum
	self._maxNum = maxNum
end
function ClassRef:getCompData()
	return self._gotNum, self._maxNum
end

function ClassRef:clone()
	local panel = ClassRef.create(self._res)
	panel:setNewTag(self:getTag())
	panel:setData(self._data)
	panel:setCompData(self._gotNum, self._maxNum)
	return panel
end

return ClassRef